package org.example.controller.user;

import org.example.annotation.HasRole;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.dto.user.CalenderDto;
import org.example.dao.entity.user.UStudent;
import org.example.dao.vo.CourseVo;
import org.example.service.user.JwServiceImpl;
import org.example.skytool.commom.ParamUtil;
import org.example.skytool.jw.dao.Exam;
import org.example.skytool.jw.dao.PscjInfo;
import org.example.skytool.jw.dao.Score;
import org.example.skytool.jw.dao.WeekCourse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 22449
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class JwController {

    /**
     * 服务对象
     */
    @Resource
    private JwServiceImpl jwService;

    /**
     * 绑定教务账号
     */
    @PostMapping("bindJw")
    public Result bindJw(@RequestBody CourseVo courseVo){
        String stuNum = courseVo.getStuNum();
        String password = courseVo.getPassword();

        ParamUtil.StringParam(stuNum,password);

        UStudent student = jwService.bindJw(stuNum,password);
        return new Result(ResultCode.SUCCESS,student);
    }

    /**
     * 解绑教务
     */
    @PostMapping("unbindJw")
    public Result unbindJw(){
        jwService.unbindJw();
        return new Result(ResultCode.SUCCESS,null);
    }

    /**
     * 判断
     */
    @PostMapping("judge")
    public Result judge(){
        Boolean flag = jwService.judge();
        return new Result(ResultCode.SUCCESS,flag);
    }

    /**
     * 获取所有学期列表(用户)
     */
    @PostMapping("/getAllSemester")
    @HasRole(role = "用户")
    public Result getAllSemester() {
        List<String> list = jwService.getAllSemester();
        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 综合获取课表信息，结合了日期和课表
     */
    @PostMapping("getWeekInfo")
    public Result getWeekInfo(@RequestBody CourseVo courseVo){
        Result result = getWeekDataInfo(courseVo);
        Result result1 = getCourse(courseVo);
        Map<String ,Object> map = new HashMap<>();
        map.put("nowWeekInfo",result.getData());
        map.put("courses",result1.getData());
        return new Result(ResultCode.SUCCESS,map);
    }

    /**
     * 获取周次日期信息(用户)
     * 例如传入周次为3 返回 对应的日期和时间
     */
    @PostMapping("getWeekDataInfo")
    @HasRole(role = "用户")
    public Result getWeekDataInfo(@RequestBody CourseVo courseVo){
        Integer nowWeek = courseVo.getZc();
        List<CalenderDto> list = jwService.getWeekDataInfo(nowWeek);
        return new Result(ResultCode.SUCCESS,list);
    }


    /**
     * 获取指定学期指定周的课表(用户)
     */
    @PostMapping("/getCourse")
    @HasRole(role = "用户")
    public Result getCourse(@RequestBody CourseVo courseVo) {
        String xueqi = courseVo.getXueqi();
        int zc = courseVo.getZc();
        WeekCourse[][] weekCourses = jwService.getCourse(xueqi, zc);
        return new Result(ResultCode.SUCCESS, weekCourses);
    }

    /**
     * 获取页面加载初始化信息
     */
    @PostMapping("/getBasicData")
    @HasRole(role = "用户")
    public Result getBasicData() {
        Map<String, Object> map = jwService.getBasicData();
        return new Result(ResultCode.SUCCESS, map);
    }




























    /**
     * 获取考试安排(用户)
     */
    @PostMapping("/getKsap")
    @HasRole(role = "用户")
    public Result getExamArrangement(@RequestBody CourseVo courseVo) {
        String xueqi = courseVo.getXueqi();
        List<Exam> list = jwService.getExamArrangement(xueqi);
        return new Result(ResultCode.SUCCESS, list);
    }


    /**
     * 查询成绩(用户)
     */
    @PostMapping("/queryScore")
    @HasRole(role = "用户")
    public Result queryScore(@RequestBody CourseVo courseVo) {
        String xueqi = courseVo.getXueqi();
        List<Score> list = jwService.getScoreList(xueqi);
        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 查询成绩详情(用户)
     */
    @PostMapping("/queryPscj")
    @HasRole(role = "用户")
    public Result queryUsualGrades(@RequestBody CourseVo courseVo) {
        String pscjUrl = courseVo.getPscjUrl();
        PscjInfo pscjInfo = jwService.getScoreDetail(pscjUrl);
        return new Result(ResultCode.SUCCESS, pscjInfo);
    }

}
